package com.baijia;

/**
 * Created by xiaohui on 2021/4/22 10:52 上午
 *
 * @author fuxiaohui01
 */
public class DeleteDuplicateNode {
    static class LinkNode {
        int val;
        LinkNode next;

        public LinkNode(int val, LinkNode next) {
            this.val = val;
            this.next = next;
        }

        public LinkNode(int val) {
            this.val = val;
        }
    }

    public static LinkNode deleteDuplicateElement(LinkNode head) {
        LinkNode newHead = new LinkNode(0);
        LinkNode preNode = newHead;
        while (head != null) {
            LinkNode p = head.next;
            if (p != null && head.val == p.val) {
                int d = head.val;
                while (head != null && d == head.val) {
                    head = head.next;
                }
            } else {
                newHead.next = head;
                newHead = head;
                head = p;
            }
        }
        newHead.next = null;
        return preNode.next;
    }

    public static void main(String[] args) {
        LinkNode linkNode = new LinkNode(1,new LinkNode(2,new LinkNode(2,new LinkNode(2,new LinkNode(4,new LinkNode(5,null))))));
        LinkNode linkNode1 = deleteDuplicateElement(linkNode);
        LinkNode p = linkNode1;
        while (p!=null){
            System.out.print(p.val+" ");
            p = p.next;
        }
    }

}
